<?php
declare (strict_types=1);

namespace app\admin\service\finance;

use app\admin\logic\finance\Refund as RefundLogic;
use app\admin\logic\system\Config as SystemConfig;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;

/**
 * Class Refund
 *
 * @package app\admin\service\finance
 */
class Refund extends BaseService
{
    /**
     * @Inject()
     *
     * @var RefundLogic
     */
    private $refundLogic;

    /**
     * 待审核列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2022/1/21
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function wait()
    {
        $map  = [
            ['status', '=', 0]
        ];
        $data = $this->refundLogic->getList($map);

        // 定义字段
        $keyList = [
            'order_no'     => ListItem::make()->title('订单号'),
            'user'         => ListItem::make()->title('退款申请用户手机号'),
            'order_title'  => ListItem::make()->title('退款项目'),
            'payment'      => ListItem::make('custom')->title('退款金额'),
            'payment_type' => ListItem::make()->title('退款到账方式'),
            'payment_time' => ListItem::make()->title('订单支付时间'),
            'create_time'  => ListItem::make()->title('申请退款时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 已通过列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2022/1/21
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function done()
    {
        $map  = [
            ['status', '=', 1]
        ];
        $data = $this->refundLogic->getList($map);

        // 定义字段
        $keyList = [
            'order_no'     => ListItem::make()->title('订单号'),
            'user'         => ListItem::make()->title('退款申请用户手机号'),
            'order_title'  => ListItem::make()->title('退款项目'),
            'payment'      => ListItem::make('custom')->title('退款金额'),
            'payment_type' => ListItem::make()->title('退款到账方式'),
            'payment_time' => ListItem::make()->title('订单支付时间'),
            'create_time'  => ListItem::make()->title('申请退款时间'),
            'update_time'  => ListItem::make()->title('处理时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 已驳回列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2022/1/21
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function reject()
    {
        $map  = [
            ['status', '=', 2]
        ];
        $data = $this->refundLogic->getList($map);

        // 定义字段
        $keyList = [
            'order_no'     => ListItem::make()->title('订单号'),
            'user'         => ListItem::make()->title('退款申请用户手机号'),
            'order_title'  => ListItem::make()->title('退款项目'),
            'payment'      => ListItem::make('custom')->title('退款金额'),
            'payment_type' => ListItem::make()->title('退款到账方式'),
            'payment_time' => ListItem::make()->title('订单支付时间'),
            'create_time'  => ListItem::make()->title('申请退款时间'),
            'update_time'  => ListItem::make()->title('处理时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 处理通过操作
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2022/1/21
     *
     * @param string $orderNo
     * @return \think\Response
     */
    public function handleDone(string $orderNo)
    {
        $this->refundLogic->handleDone($orderNo);
        return $this->parseToData([], 1, '处理成功');
    }

    /**
     * 处理驳回操作
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2022/1/21
     *
     * @param string $orderNo
     * @return \think\Response
     */
    public function handleReject(string $orderNo)
    {
        $this->refundLogic->handleReject($orderNo);
        return $this->parseToData([], 1, '处理成功');
    }

    /**
     * 退款配置
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2022/1/22
     *
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function config()
    {
        $fields        = [
            'effective_days' => FormItem::make('inputNumber')->title('退款有效天数')->min(0)->required()->help('1. 订单支付后（从支付当天0点计算）多少天内可退款<br />2. 0表示不限制退款时间'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'refund.config';
        $config          = SystemConfig::get($key, ['effective_days' => 0]);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();
    }

    /**
     * 管理员操作退款
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/10
     *
     * @param string $orderSubNo
     * @return mixed
     */
    public function orderRefundByAdmin(string $orderSubNo)
    {
        $this->refundLogic->orderRefundByAdmin($orderSubNo);

        return $this->parseToData([], 1, '处理成功');
    }
}